package com.hxrd.androidprint.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Created by elliott on 16/9/1.
 * 文件的管理工具类
 */
public class FileUtils {

    public static String file_name = "local_file";


    public static String SDPATH = Environment.getExternalStorageDirectory()
            + "/sdcard/failMsg/";

    /**
     * 把一个字符串存成一个文件
     * @param context
     * @param data
     */
    public static void saveDataToFile(Context context, String data) {
        FileOutputStream fileOutputStream;
        BufferedWriter writer;

        try {
            // 打开文件的输出流
            // 文件名, 文件的模式
            // 默认存储在  files文件夹下面
            // Context.MODE_APPEND 追加模式  在文件内容的后面添加内容
            // Context.MODE_PRIVATE 覆盖模式 覆盖上次的内容
            fileOutputStream = context.openFileOutput(file_name,
                    Context.MODE_PRIVATE);
            // 初始化  BufferedWriter
            // OutputStreamWriter 输出流的写入类
            writer = new BufferedWriter(
                    new OutputStreamWriter(fileOutputStream));
            // 写入数据
            writer.write(data);

            // 关闭相关的流
            writer.close();
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 文件的读取
     * @param context
     * @param filename
     * @return
     */
    public static String readFile(Context context, String filename) {

        FileInputStream stream;
        BufferedReader reader;
        // 存储最后读出来的结果
        StringBuilder stringBuilder = new StringBuilder();

        try {
            // 获取对应的文件流
            stream = context.openFileInput(filename);
            // 实例化 BufferedReader
            reader = new BufferedReader(
                    new InputStreamReader(stream));
            // 每次读取一行的数据
            String lineStr = "";
            // 循环取出每行
            while ((lineStr = reader.readLine()) != null) {
                stringBuilder.append(lineStr);
            }

            // 关闭相关的流
            reader.close();
            stream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }


    //读取文本文件中的内容
    public static String ReadTxtFile(String strFilePath)
    {
        String path = strFilePath;
        String content = ""; //文件内容字符串
        //打开文件
        File file = new File(path);
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory())
        {
            Log.d("TestFile", "The File doesn't not exist.");
        }
        else
        {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null)
                {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while (( line = buffreader.readLine()) != null) {
                        content += line + "\n";
                    }
                    instream.close();
                }
            }
            catch (java.io.FileNotFoundException e)
            {
                Log.d("TestFile", "The File doesn't not exist.");
            }
            catch (IOException e)
            {
                Log.d("TestFile", e.getMessage());
            }
        }
        return content;
    }



    //删除文件
    public static void delFile(String fileName){
        File file = new File(SDPATH + fileName);
        if(file.isFile()){
            file.delete();
        }
        file.exists();
    }

}
